<form id="buletin-form" method="POST" action="<?php echo site_url("publikasi/upload/buletin"); ?>">
    <div>
        Edisi :
        <input id="buletin-ed" type="text" name="ed" style="width: 2em;" />
    </div>
    <div>
        File (PDF, maks. 5MB) :
    </div>
    <div>
        <input type="file" name="file" accept="application/pdf" />
    </div>
</form>
<form id="buletin-prev-form" method="POST" action="<?php echo site_url("publikasi/upload/buletin/true"); ?>">
    <div>
        File Preview (PDF, maks. 1MB) :
        <input id="buletin-ed-prev" type="hidden" name="ed" />
    </div>
    <div>
        <input id="prev-file" type="file" name="file" accept="application/pdf" />
    </div>
</form>
<div>
    <button id="submit-btn" onClick="javascript:saveForm();">Upload</button>
</div>
<script type="text/javascript">
        jQuery(document).ready(function() {
            // submit Form
            $("#buletin-form").ajaxForm({
                beforeSend: function()
                {
                    $("#submit-btn").prop("disabled", true);
                    $("#submit-btn").text("Mengupload...");
                },
                uploadProgress: function(event, position, total, percentComplete)
                {
                    $("#submit-btn").text("Mengupload... " + percentComplete + " %");
                },
                error: function() {
                    alert("Error");
                    $("#submit-btn").prop("disabled", false);
                    $("#submit-btn").text("Upload");
                },
                complete: function(response)
                {
                    var resObj = $.parseJSON(response.responseText);
                    if (resObj.isSuccess) {
                        if ($("#prev-file").val() !== "") {
                            savePrevForm();
                        }
                    } else {
                        alert("Error");
                        $("#submit-btn").prop("disabled", false);
                        $("#submit-btn").text("Upload");
                    }
                }
            });
            $("#buletin-prev-form").ajaxForm({
                beforeSend: function()
                {
                    $("#submit-btn").prop("disabled", true);
                    $("#submit-btn").text("Mengupload preview...");
                },
                uploadProgress: function(event, position, total, percentComplete)
                {
                    $("#submit-btn").text("Mengupload preview... " + percentComplete + " %");
                },
                error: function() {
                    alert("Error");
                    $("#submit-btn").prop("disabled", false);
                    $("#submit-btn").text("Upload");
                },
                complete: function(response)
                {
                    var resObj = $.parseJSON(response.responseText);
                    if (resObj.isSuccess) {
                        window.location = "<?php echo site_url("usercp/managepub/buletin"); ?>";
                    } else {
                        alert("Error");
                        $("#submit-btn").prop("disabled", false);
                        $("#submit-btn").text("Upload");
                    }
                }
            });

        });

        function savePrevForm() {
            var edInt = parseInt($("#buletin-ed").val(), 10);
            if (isNaN(edInt) || (edInt < 1)) {
                alert("Silahkan masukkan nomor edisi dengan benar");
                $("#buletin-ed").focus();
                return false;
            }
            $("#buletin-ed-prev").val(edInt);
            $("#buletin-prev-form").submit();
        }

        function saveForm() {
            // check edisi
            var edInt = parseInt($("#buletin-ed").val(), 10);
            if (isNaN(edInt) || (edInt < 1)) {
                alert("Silahkan masukkan nomor edisi dengan benar");
                $("#buletin-ed").focus();
                return false;
            }
            $("#buletin-form").submit();
        }
</script>